<?php 

class Xml extends File{

    protected $_encode;
    protected $_inEncode;
    protected $_outEncode;

    protected static $templateXml     = array(
        'gbk' => '<?xml version="1.0" encoding="gb2312"?>%s',
        'utf8' => '<?xml version="1.0" encoding="utf-8"?>%s' 
    );

    protected static $encodeMap     = array(
        'gbk' => 'gb2312',
        'utf8' => 'UTF-8'
    );

    public function __construct($filePath, $content = '', $inEncode = 'utf8', $outEncode = '') {
        parent::__construct($filePath, $content);
        
        $this->_encode      = $inEncode;
        $this->_inEncode    = self::$encodeMap[$inEncode];
        $this->_outEncode   = self::$encodeMap[$outEncode];
    }

    public function saveFile() {
        $content            = $this->encodeContent();
        $this->_content     = sprintf(self::$templateXml[$this->_encode], $content);

        return parent::saveFile();
    }

    protected function encodeContent() {
        if(($this->_inEncode&&$this->_outEncode) && $this->_inEncode != $this->_outEncode) {
            return iconv($this->_inEncode, $this->_outEncode, $this->_content);
        }

        return $this->_content;
    }

}

?>
